import java.util.Scanner;

public class Administrator extends User {
    boolean passwordFlag = true;
    boolean logInFlag = false;
    CustomerSet customerSet;
    GoodsSet goodsSet = new GoodsSet();

    Administrator(CustomerSet customerSet) {
        setAccount("admin");
        setPassword("ynuinfo#777");
        setCustomerSet(customerSet);
    }//
    public void menu() {
        Scanner input = new Scanner(System.in);
        System.out.println("请选择要进行的操作");
        System.out.println("1--登录");
        System.out.println("2--密码管理");
        System.out.println("3--客户管理");
        System.out.println("4--商品管理");
        System.out.println("5--返回上一级/退出登录");
        int choose = 0;
        try {
            choose = input.nextInt();
        } catch (Exception e) {
            System.out.println("输入不合法，请重新输入。");
            while (input.hasNext()) {
                if (input.hasNextLine()) {
                    input.nextLine();
                    break;
                } else {
                    input.next();
                }
            }
            menu();
            return;
        }
        switch (choose) {
            case 1:
                logIn();
                menu();
                return;
            case 2:
                if (!logInFlag) {
                    System.out.println("请先登录");
                    System.out.println("输入任意字符返回");
                    input.next();
                    menu();
                    return;
                }
                passwordManagement();
                break;
            case 3:
                if (!logInFlag) {
                    System.out.println("请先登录");
                    System.out.println("输入任意字符返回");
                    input.next();
                    menu();
                    return;
                }
                customerManagement();
                menu();
                return;
            case 4:
                if (!logInFlag) {
                    System.out.println("请先登录");
                    System.out.println("输入任意字符返回");
                    input.next();
                    menu();
                    return;
                }
                goodsManagement();
                break;
            case 5:
                logInFlag = false;
                return;
            default:
                System.out.println("输入不合法，请重新输入。");
                menu();
                return;
        }

    }//
    public void passwordManagement() {
        Scanner input = new Scanner(System.in);
        System.out.println("请选择你要进行的操作：");
        System.out.println("1--修改密码");
        System.out.println("2--重置指定客户/用户的密码(初始密码为12345abcde)");
        System.out.println("3--返回上一级。");
        int choose = 0;
        try {
            choose = input.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新输入!!");
            while (input.hasNext()) {
                if (input.hasNextLine()) {
                    input.nextLine();
                    break;
                } else {
                    input.next();
                }
            }
            passwordManagement();
            return;
        }
        switch (choose) {
            case 1:
                changePassword();
                passwordManagement();
                return;
            case 2:
                resetCustomerPassword();
                passwordManagement();
                return;
            case 3:
                menu();
                return;
            default:
                System.out.println("输入不合法，请重新输入。");
                passwordManagement();
                return;
        }
    }//
    public void customerManagement() {
        Scanner input = new Scanner(System.in);
        System.out.println("请选择你要进行的操作：---------------------------------------------------------------------------");
        System.out.println("1--查看客户信息");
        System.out.println("2--删除客户信息");
        System.out.println("3--查询客户信息");
        System.out.println("4--返回上一级");
        int choose = 0;
        try {
            choose = input.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新输入。");
            while (input.hasNext()) {
                if (input.hasNextLine()) {
                    input.nextLine();
                    break;
                } else {
                    input.next();
                }
            }
            customerManagement();
            return;
        }
        switch (choose) {
            case 1:
                showCustomers();
                System.out.println("输入任意字符继续");
                input.next();
                customerManagement();
                break;
            case 2:
                deleteCustomer();
                System.out.println("输入任意字符继续");
                input.next();
                customerManagement();
                break;
            case 3:
                searchCustomer();
                System.out.println("输入任意字符继续");
                input.next();
                customerManagement();
                break;
            case 4:
                menu();
                return;
            default:
                System.out.println("输入不合法，请重新输入。");
                customerManagement();
                break;
        }
    }

    public void goodsManagement() {
        Scanner input = new Scanner(System.in);
        System.out.println("请选择你要进行的操作：");
        System.out.println("1--展示商品信息");
        System.out.println("2--添加商品的信息");
        System.out.println("3--修改商品的信息");
        System.out.println("4--删除商品的信息");
        System.out.println("5--查询商品的信息");
        System.out.println("6--返回上一级");
        int choose = 0;
        try {
            choose= input.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新输入。");
            while (input.hasNext()) {
                if (input.hasNextLine()) {
                    input.nextLine();
                    break;
                } else {
                    input.next();
                }
            }
            goodsManagement();
            return;
        }
        switch (choose) {
            case 1:
                showGoods();
                System.out.println("输入任意字符继续");
                input.next();
                goodsManagement();
                break;
            case 2:
                addGood();
                System.out.println("输入任意字符继续");
                input.next();
                goodsManagement();
                break;
            case 3:
                changeGood();
                System.out.println("输入任意字符继续");
                input.next();
                goodsManagement();
                break;
            case 4:
                deleteGood();
                System.out.println("输入任意字符继续");
                input.next();
                goodsManagement();
                break;
            case 5:
                searchGood();
                goodsManagement();
                break;
            case 6:
                menu();
                return;
            default:
                System.out.println("输入不合法，请重新输入。");
                goodsManagement();
                break;
        }
    }
    private void logIn() {
        Scanner input = new Scanner(System.in);
        if (logInFlag) {
            System.out.println("您已登录。");
            System.out.println("输入任意字符返回");
            input.next();
            return;
        }
        System.out.println("请输入账户名：");
        if (!input.next().equals(getAccount())) {
            System.out.println("不存在该账户，请重新输入。(输入任何字符继续,输入back返回上一级。)");
            String temp = input.next();
            if (temp.equals("back")) return;
            logIn();
            return;
        }
        System.out.println("请输入密码：");
        if (!input.next().equals(getPassword())) {
            System.out.println("账户和密码不匹配，请重新输入。(输入任何数字继续,输入back返回上一级。)");
            String temp = input.next();
            if (temp.equals("back")) return;
            logIn();
            return;
        }
        System.out.println("登录成功，输入任何字符继续");
        logInFlag = true;
    }
    public void changePassword() {
        if (!passwordFlag) {
            System.out.println("密码输入错误次数过多，请明天尝试。");
            return;
        }
        Scanner input = new Scanner(System.in);
        for (int ii = 0; ii < 3; ii++) {
            System.out.println("请输入原密码：");
            String tempPassword = input.next();
            if (tempPassword.equals(getPassword())) {
                while (true) {
                    System.out.println("请输入新密码：");
                    String newPassword1 = input.next();
                    System.out.println("请确认新密码：");
                    String newpassword2 = input.next();
                    if (newpassword2.equals(newPassword1)) {
                        setPassword(newPassword1);
                        System.out.println("设置成功！！！");
                        return;
                    } else {
                        System.out.println("输入错误，请重新输入。");
                    }
                }
            } else {
                System.out.println("密码错误，请重新输入,剩余次数：" + (2 - ii));
            }
        }
        passwordFlag = false;
        System.out.println("密码输入错误次数过多，请明天尝试。");
    }//
    public void resetCustomerPassword() {
        Scanner input = new Scanner(System.in);
        if(customerSet.customerList.isEmpty()){
            System.out.println("不存在用户，请先创建用户。");
            return;
        }
        System.out.println("请选择要重置的顾客：");
        int cnt = 0;
        for (Customer cus : customerSet.customerList) {
            System.out.println(++cnt + " " + cus.getAccount() + "  ");
        }
        int idx = 0;
        try {
            idx = input.nextInt();
            customerSet.customerList.get(idx - 1).resetPassword();
        }catch (Exception e){
            System.out.println("输入不合法，请重新输入。");
            while (input.hasNext()) {
                if (input.hasNextLine()) {
                    input.nextLine();
                    break;
                } else {
                    input.next();
                }
            }
            resetCustomerPassword();
            return;
        }
        System.out.println("重置成功！！！");
    }
    public void showCustomers() {
        if(customerSet.customerList.isEmpty()){
            System.out.println("还不存在用户，请先创建用户。");
            return;
        }
        for (Customer cus : customerSet.customerList) {
            cus.show();
        }
    }
    public void deleteCustomer() {
        if(customerSet.customerList.isEmpty()){
            System.out.println("还不存在用户，请先创建用户。");
            return;
        }
        Scanner input = new Scanner(System.in);
        System.out.println("请选择要删除的客户：");
        int idx = 0;
        for (Customer cus : customerSet.customerList) {
            System.out.println(++idx + "  客户ID:" + cus.ID + "用户名:" + cus.getAccount());
        }
        int deleteIdx = 0;
        try {
            deleteIdx = input.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新输入。");
            while (input.hasNext()) {
                if (input.hasNextLine()) {
                    input.nextLine();
                    break;
                } else {
                    input.next();
                }
            }
            deleteCustomer();
            return;
        }
        System.out.println("删除后无法恢复，确定要删除吗？（输入yes删除）");
        String or = input.next();
        if (!or.equals("yes")) {
            System.out.println("已取消删除");
            return;
        }
        customerSet.customerList.remove(deleteIdx - 1);
        System.out.println("删除成功！！！");
    }

    public void addGood() {
        goodsSet.addGood();
    }

    public void searchGood() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("1--根据商品名称单独查询");
        System.out.println("2--根据生产厂家、零售价格组合查询");
        System.out.println("3--返回上一级。");
        int choose = 0;
        try {
            choose = scanner.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新输入。");
            while (scanner.hasNext()) {
                if (scanner.hasNextLine()) {
                    scanner.nextLine();
                    break;
                } else {
                    scanner.next();
                }
            }
            searchGood();
            return;
        }

        switch (choose) {
            case 1:
                goodsSet.searchGoodsWithName();
                break;
            case 2:
                goodsSet.setGoodsWithTimeAddPos();
                break;
            case 3:
                return;
            default:
                System.out.println("输入不合法，请重新输入。");
                searchGood();
                break;

        }


    }

    public void deleteGood() {
        Scanner input = new Scanner(System.in);
        System.out.println("请选择要删除的商品：");
        goodsSet.showLittle();
        int choose = 0;
        try {
            choose = input.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新输入。");
            while (input.hasNext()) {
                if (input.hasNextLine()) {
                    input.nextLine();
                    break;
                } else {
                    input.next();
                }
            }
            deleteGood();
            return;
        }
        System.out.println("删除后不可恢复，是否删除？（确认删除请输入yes）");
        if (input.next().equals("yes")) {
            goodsSet.goodsList.remove(choose - 1);
            System.out.println("删除成功！！！");
        } else {
            System.out.println("已取消删除。");
        }
    }

    public void changeGood() {
        Scanner input = new Scanner(System.in);
        System.out.println("请选择修改的商品：");
        for (int i = 0; i < goodsSet.goodsList.size(); i++) {
            System.out.print(i + 1 + "  ");
            goodsSet.goodsList.get(i).showLittle();
            System.out.println();
        }
        int idx = 0;
        try {
            idx = input.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新输入。");
            while (input.hasNext()) {
                if (input.hasNextLine()) {
                    input.nextLine();
                    break;
                } else {
                    input.next();
                }
            }
            changeGood();
            return;
        }
        goodsSet.goodsList.get(idx - 1).change();

    }



    public void showGoods() {
        for (Good good : goodsSet.goodsList) {
            good.show();
        }
    }

    public void searchCustomer() {
        Scanner input = new Scanner(System.in);
        boolean flag = false; //查询的输入是否存在
        System.out.println("请输入要查询顾客的id或账户名");
        String str = input.next();
        for (Customer cus : customerSet.customerList) {
            if (str.equals(cus.getAccount()) || str.equals(cus.ID)) {
                flag = true;
                System.out.println("客户ID:" + cus.ID + "   用户名:" + cus.getAccount());
            }
        }
        if (!flag) {
            System.out.println("输入的ID或账户名不存在，以下是所有用户信息：");
            for (Customer cus : customerSet.customerList) {
                System.out.println("客户ID:" + cus.ID + "用户名:" + cus.getAccount());
            }
        }
    }
    public void setCustomerSet(CustomerSet customerSet) {
        this.customerSet = customerSet;
    }

}
